//
//  BankBillView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/7.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class BankBillView: UIView {
    
    let accountView = InputView.init(inputStyle: .account) //账户
    let passwordView = InputView.init(inputStyle: .password) //密码
    
    fileprivate var isUpdate = false
    fileprivate var isAgree = false
    
    let updateCheckBtn = UIButton()
    let updateButton = UIButton()

    let agreeCheckBtn = UIButton()
    let agreeButton = UIButton()
    
    let openButton = UIButton()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(accountView)
        
        passwordView.placeholder = signInPassword
        self.addSubview(passwordView)
        
        updateCheckBtn.layer.borderColor = UIColor.black.cgColor
        updateCheckBtn.layer.borderWidth = 1
        updateCheckBtn.addTarget(self, action: #selector(selectUpdate(sender:)), for: .touchUpInside)
        self.addSubview(updateCheckBtn)
        
        updateButton.setTitle(automaticUpdates, for: .normal)
        updateButton.setTitleColor(UIColor.black, for: .normal)
        updateButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        updateButton.addTarget(self, action: #selector(selectUpdate(sender:)), for: .touchUpInside)
        self.addSubview(updateButton)
        
        agreeCheckBtn.layer.borderColor = UIColor.black.cgColor
        agreeCheckBtn.layer.borderWidth = 1
        agreeCheckBtn.addTarget(self, action: #selector(agreeAction(sender:)), for: .touchUpInside)
        self.addSubview(agreeCheckBtn)
        
        agreeButton.setTitle(agreeAgreement, for: .normal)
        agreeButton.setTitleColor(UIColor.black, for: .normal)
        agreeButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        agreeButton.addTarget(self, action: #selector(agreeAction(sender:)), for: .touchUpInside)
        self.addSubview(agreeButton)
        
        openButton.backgroundColor = UIColor.lightGray
        openButton.setTitle(openOnlineBanking, for: .normal)
        openButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        openButton.layer.cornerRadius = RATIO_H(maxNum: 20)
        openButton.layer.masksToBounds = true
        self.addSubview(openButton)
        
        accountView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 10))
            make.height.equalTo(RATIO(maxNum: 50))
        }
        
        passwordView.snp.makeConstraints { (make) in
            make.top.equalTo(accountView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.left.right.equalToSuperview()
            make.height.equalTo(accountView)
        }
        
        
        updateCheckBtn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(RATIO(maxNum: 20))
            make.top.equalTo(passwordView.snp.bottom).offset(RATIO_H(maxNum: 20))
            make.width.height.equalTo(RATIO_H(maxNum: 15))
        }
        
        updateButton.snp.makeConstraints { (make) in
            make.left.equalTo(updateCheckBtn.snp.right).offset(RATIO(maxNum: 5))
            make.centerY.equalTo(updateCheckBtn)
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        agreeButton.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -20))
            make.centerY.equalTo(updateCheckBtn)
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        agreeCheckBtn.snp.makeConstraints { (make) in
            make.right.equalTo(agreeButton.snp.left).offset(RATIO(maxNum: -5))
            make.centerY.equalTo(agreeButton)
            make.width.height.equalTo(RATIO_H(maxNum: 15))
        }
        
        openButton.snp.makeConstraints { (make) in
            make.top.equalTo(updateCheckBtn.snp.bottom).offset(RATIO_H(maxNum: 20))
            make.left.equalToSuperview().offset(RATIO(maxNum: 20))
            make.right.equalToSuperview().offset(RATIO(maxNum: -20))
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
        
    }
    
    //MARK: 选择自动更新
    @objc fileprivate func selectUpdate(sender:UIButton) {
        
        if self.isUpdate {
            self.isUpdate = false
            updateCheckBtn.layer.contents = UIImage.imageWithName("checked").cgImage
        }else{
            self.isUpdate = true
            updateCheckBtn.layer.contents = UIImage()
        }
    }
    
    //MARK: 同意用户协议
    @objc fileprivate func agreeAction(sender:UIButton) {
        
        if self.isAgree {
            self.isAgree = false
            agreeCheckBtn.layer.contents = UIImage.imageWithName("checked").cgImage
        }else{
            self.isAgree = true
            agreeCheckBtn.layer.contents = UIImage()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
